<?php

/**
 * @author Terry
 * @copyright 2009
 */

require_once("class.php");
require_once("config.php");
session_start();
$talkhistory = $_SESSION['extrainfo']['talkhistory'];
$warhistory = $_SESSION['extrainfo']['warhistory'];
if(count($talkhistory) > 0) {
	foreach($talkhistory as $th) {
		$sql = sprintf("insert ignore into spectalk (type,sid,uuid,content,timeline) values ('%s','%s','%s','%s','%s')",$th[0],$th[1],$th[2],$th[3],$th[4]);
		$db->insert($sql);
	}
}

$sidea = '';
foreach($_SESSION['a'] as $sa) {
	$sidea .= $sa->cid.',';
}
$sidea = rtrim($sidea,',');
$sideb = '';
foreach($_SESSION['b'] as $sb) {
	$sideb .= $sb->cid.',';
}
$sideb = rtrim($sideb,',');
if(CalHelper::live_count($_SESSION['a'],0) == 0) {
	$winner = 1;
} elseif(CalHelper::live_count($_SESSION['b'],0) == 0) {
	$winner = 0;
} else {
	$winner = 2;
}
$war_uuid = md5($sidea.$sideb.$warhistory['startime']);
$sql = sprintf("insert ignore into warhistory (sidea,sideb,uuid,endtalk,winner,startime,endtime) values ('%s','%s','%s','%s','%s','%s','%s')",
	$sidea,$sideb,$war_uuid,$_SESSION['show'],$winner,$warhistory['startime'],date('Y-m-d H:i:s')); 
$db->insert($sql);
$_SESSION['extrainfo']['saved'] = 1;
?>
<html>
<head>
        <title>战历保存</title>
        <meta http-equiv='Content-Type' content="text/html; charset=utf-8">
        <meta http-equiv='Refresh' content="5; battle.php">
</head>
战历保存完成,5秒之后跳回原页面...<br>
</html>
